package main

import (
	"os"

	"gogs.xiaoyuanjijiehao.com/antlinker/antcms/debug"
	"gogs.xiaoyuanjijiehao.com/antlinker/antcms/log"

	"gopkg.in/urfave/cli.v2"
)

func createSrvFlag() []cli.Flag {
	return []cli.Flag{
		&cli.StringFlag{
			Name:    "config",
			Aliases: []string{"c"},
			Value:   "./conf/conf.yaml",
			Usage:   "服务配置文件",
		},

		&cli.StringFlag{
			Name:    "pprof",
			Aliases: []string{"p"},
			Value:   "",
			Usage:   "启动pprof分析程序,默认不启动，可以设置:9090,指定一个监听ip端口通过http访问",
		},
	}
}
func createSrv() *cli.Command {
	return &cli.Command{

		Name:    "server",
		Aliases: []string{"srv"},
		Usage:   "启动内容信息发布核心服务",
		Flags:   createSrvFlag(),
		Action: func(c *cli.Context) error {
			pprof(c)
			cfg, err := parseConfig(c.String("config"))
			if err != nil {
				return err
			}

			return runSrv(*cfg)
		},
	}
}
func main() {
	app := &cli.App{
		Authors: []*cli.Author{
			{Name: "@antlinker.com"},
		},
		Name:    "antcms",
		Version: "1.0",
		Usage:   "antcms 内容信息发布服务",
		Flags: []cli.Flag{
			&cli.StringFlag{
				Name:    "bindir",
				Aliases: []string{"bin"},
				Value:   "./",
				Usage:   "启动程序所在的目录",
			},
		},
		Commands: []*cli.Command{
			createSrv(),
		},
	}
	err := app.Run(os.Args)
	if err != nil {
		log.Error("启动失败:", err)
	}
}

func pprof(c *cli.Context) {

	pprofaddr := c.String("pprof")
	if pprofaddr != "" {
		debug.StartHTTPPprof(pprofaddr)
	}
}
